/**
 * 题目：https://leetcode-cn.com/problems/add-two-numbers/
 * 题解：https://shentuzhigang.blog.csdn.net/article/details/116174387
 * 
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int sum = 0;
        ListNode head = l1;
        while (l1 != null) {
            if (l2 != null) {
                sum = l1.val + l2.val;
                l2 = l2.next;
                if (l1.next == null) {
                    l1.next = l2;
                    l2 = null;
                }
            } else {
                sum = l1.val;
                if (sum / 10 == 0) {
                    break;
                }
            }
            l1.val = sum % 10;
            if (sum / 10 > 0) {
                if (l1.next == null) {
                    l1.next = new ListNode(sum / 10);
                    break;
                } else {
                    l1.next.val = l1.next.val + sum / 10;
                }
            }
            l1 = l1.next;
        }
        return head;
    }
}
